package com.example.demo.controller;

import com.example.demo.pojo.IndividualUser;
import com.example.demo.service.IndividualUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;

@RestController
@RequestMapping("/individualUser")
public class IndividualUserController {
    @Autowired
    private IndividualUserService individualUserService;


    //账号修改 /individualUser/updateIntegral
    @PostMapping("/updateIntegral")
    public Object updateIntegral(@RequestBody IndividualUser individualUser){
        HashMap<String,Object> map = new HashMap();
        int i = individualUserService.updateIntegral(individualUser);
        if (i>0){
            map.put("code",200);
            map.put("msg","修改成功");
        }else {
            map.put("code",300);
            map.put("msg","修改失败");
        }
        return map;
    }

    //登录 /individualUser/login
    @PostMapping("/login")
    public Object login(@RequestBody IndividualUser individualUser){
        HashMap<String,Object> map = new HashMap();
        try {
            IndividualUser user = individualUserService.login(individualUser);
            if (user!=null){
                map.put("code",200);
                map.put("msg","登录成功");
                map.put("data",user);
            }else {
                map.put("code",300);
                map.put("msg","账号或密码错误");
            }
        }catch (Exception exception){
            map.put("code",400);
            map.put("msg","登录失败");
            map.put("err",exception);
        }finally {
            return map;
        }
    }

    //注册 /individualUser/register
    @PostMapping("/register")
    public Object register(@RequestBody IndividualUser individualUser){
        HashMap<String,Object> map = new HashMap();
//        individualUser.setId(individualUser.getPhone()+"");
        try {
            int i = individualUserService.addIndividualUser(individualUser);
            if (i > 0) {
                map.put("code",200);
                map.put("msg","注册成功");
            }else {
                map.put("code",300);
                map.put("msg","注册失败");
            }
        }catch (Exception exception){
            map.put("code",400);
            map.put("msg","注册失败");
        }finally {
            return map;
        }
    }
}
